package com.zhaoyizhe.demo.controller;

import com.zhaoyizhe.demo.constant.ConstantSessionId;
import com.zhaoyizhe.demo.enums.ResultCodeEnum;
import com.zhaoyizhe.demo.enums.StatusEnum;
import com.zhaoyizhe.demo.model.*;
import com.zhaoyizhe.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo,HttpSession session) {
        log.info("BookController.addBook: 接收参数: {}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName()) ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                bookInfo.getCount() == null ||
                bookInfo.getPrice() == null ||
                !StringUtils.hasLength(bookInfo.getPublish())) {
            log.warn("图书信息不正确！");
            return false;
        }
        Integer result = bookService.addBook(bookInfo);
        if (result == 1) {
            log.info("添加图书成功！");
            return true;
        }
        log.error("添加图书出错！");
        return false;
    }

    @RequestMapping("/getListPage")
    public Result<ResultPage> getListPage(RequestPage requestPage, HttpSession session) {
        log.info("BookController.getListPage:requestPage:{}",requestPage);
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstantSessionId.SESSION_ID_KEY);
        if (userInfo == null) {
            // 用户未登录
            return Result.userNoLogin();
        }
        // 用户已登录
        ResultPage data = bookService.getListPage(requestPage);
        return Result.succeed(data);
    }

    @RequestMapping("/updateGetBookInfo")
    public BookInfo updateGetBookInfo(Integer id,HttpSession session) {
        log.info("BookController.updateGetBookInfo : id :{}", id);
        BookInfo bookInfo = bookService.updateGetBookInfo(id);
        if (bookInfo == null ) {
            log.warn("bookInfo不存在");
            return null;
        }
        return bookInfo;
    }

    @RequestMapping("/updateBookInfo")
    public Boolean updateBookInfo(BookInfo bookInfo) {
        log.info("BookController.updateBookInfo :{}", bookInfo);
        if (bookInfo == null || bookInfo.getId() == null) {
            log.warn("bookId为空");
            return false;
        }
        if (!StringUtils.hasLength(bookInfo.getBookName())
            ||!StringUtils.hasLength(bookInfo.getAuthor())
            ||bookInfo.getCount() == null
            ||bookInfo.getPrice() == null
            ||!StringUtils.hasLength(bookInfo.getPublish())
            ||bookInfo.getStatus() == null) {
            log.warn("修改信息不完整");
            return false;
        }
        Integer result = bookService.updateBookInfo(bookInfo);
        if (result != 1) {
            log.error("数据库修改失败!");
            return false;
        }
        return true;
    }

    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer id) {
        log.info("BookController.deleteBook: id : {}",id);
        if (id == null) {
            log.warn("bookId为空");
            return false;
        }
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(StatusEnum.DELETED.getCode());
        try {
            Integer result = bookService.updateBookInfo(bookInfo);
            if (result < 1) return false;
        } catch(Exception e) {
            log.error("删除失败,error:",e);
            return false;
        }
        return true;
    }

    @RequestMapping("/batchDeleteBooks")
    public Boolean batchDeleteBooks(@RequestParam List<Integer> bookIds) {
        log.info("BookController.batchDeleteBooks : bookInfos:{}",bookIds);
        if (bookIds == null) {
            return true;
        }
        try {
            Integer result = bookService.batchDeleteBooks(bookIds);
            if (result > 0) return true;
        } catch (Exception e) {
            log.error("批量删除失败 error:",e);
        }
        return false;
    }

    @RequestMapping("/addPageCheckLogin")
    public void addCheckLogin() {}

}
